#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
 main()
{
	system ("color 1a");
int n, ar[100], b[100], i, j, tmp, wt[100], ta[100], time[100];
int totWT=0, totTA=0;
float AvWT, AvTA;
char name[20][20], tmpName[20];
printf("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
printf("#\t       Program Penjadwalan FCFS (FIRST COME FIRST SERVED)\t     #\n");
printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
puts("");
printf("\t\t\t    Daftar Nama Kelompok :\n");
puts("");
printf("\t\t\tAdittiya Veriawan (1310511074)\t\t\t\n");
printf("\t\t\tDwi Cahyo Utomo   (1310511081)\t\t\t\n\n");
puts("");
printf("Masukkan Angka Untuk Proses FCFS : "); scanf("%d", &n);
puts("");

for(i=0; i<n; i++){
fflush(stdin);
printf("Silahkan Masukkan Nama Untuk Proses : "); gets(name[i]);
printf("Silahkan Masukkan Angka Untuk Arrival Time : "); scanf("%d", &ar[i]);
printf("Silahkan Masukkan Angka Untuk Burst Time : "); scanf("%d", &b[i]);
puts("");
}

for(i=0; i<n; i++){
for(j=i+1; j<n; j++)
if(ar[i]>ar[j]){
strcpy(tmpName, name[i]);
strcpy(name[i], name[j]);
strcpy(name[j], tmpName);
tmp=ar[i];
ar[i]=ar[j];
ar[j]=tmp;
tmp=b[i];
b[i]=b[j];
b[j]=tmp;
}
}
time[0]=ar[0];

puts("\n\t.:: Process Table ::.");
puts("==========================================\n");
printf("| no | proses\t | Time Arrival\t | Burst |\n");
puts("------------------------------------------");
for (i=0; i<n; i++){
printf("| %2d | %s\t |  \t%d\t | %d\t |\n", i+1, name[i], ar[i], b[i]);
time[i+1]=time[i]+b[i];
wt[i]=time[i]-ar[i];
ta[i]=time[i+1]-ar[i];
totWT+=wt[i];
totTA+=ta[i];
}
puts("==========================================\n");
printf("Total Waiting Time : %d \n", totWT);
printf("Turn Arround Time  : %d \n\n", totTA);
puts("\n\t   .:: Time Process Table ::.    ");
puts("==================================================\n");
printf("| No | Proses\t | Waiting Time\t | Turn Arround\t |\n");
puts("--------------------------------------------------");
for(i=0; i<n; i++){
printf("| %2d | %s\t |  \t%d\t | \t%d\t |\n", i+1, name[i], wt[i], ta[i]);
}
puts("==================================================\n");
//Untuk Gantchart
puts("\n");
puts("\t.:: Gant-Chart ::.\n");
for(i=0; i<n; i++){
printf(" %s\t ", name[i]);
}
puts("");
for(i=0; i<n; i++){
printf("|=========");
}
printf("|\n");
for(i=0; i<=n; i++){
printf(" %d\t ", time[i]);
}
puts("\n");
AvWT=(float)totWT/n;
AvTA=(float)totTA/n;
printf("Average Waiting Time      : %f\n", AvWT);
printf("Average Turn Arround TIme : %f\n", AvTA);
getch();
}